package com.ylr.admin.framework.controller.web;

import java.util.List;
import java.util.Objects;

import com.ylr.base.common.annotation.Resubmit;
import com.ylr.admin.common.vo.role.menu.RoleMenuVo;
import com.ylr.admin.framework.service.RoleMenuService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ylr.base.common.tool.Result;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-04-21 11:45:13
 * className: RoleMenuController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/web/role-menu")
@AllArgsConstructor
public class RoleMenuWebController {

    private static final Logger log = LoggerFactory.getLogger(RoleMenuWebController.class);

    private final RoleMenuService roleMenuService;

    /**
     * 保存角色关联菜单接口
     * @param roleMenuVo 参数对象
     */
    @Resubmit(spEL = "'saveOrUpdateRoleMenu-'.concat(#roleMenuVo.roleIdList[0])", message = "保存角色关联菜单")
    @RequestMapping("/saveOrUpdate")
    public Result<Object> saveOrUpdateRoleMenu(@Validated @RequestBody RoleMenuVo roleMenuVo) {
        log.info("web => 开始保存角色关联菜单操作。{}", roleMenuVo);

        Long roleId = roleMenuVo.getRoleIdList().get(0);
        List<Long> menuIdList = roleMenuVo.getMenuIdList();
        if (Objects.isNull(roleId)) {
            log.info("角色主键值错误");
            return Result.requestFail("角色主键不能为空");
        }
        for (int i = 0; i < menuIdList.size(); i++) {
            Long menuId = menuIdList.get(i);
            if (Objects.isNull(menuId)) {
                log.info("菜单主键值错误。i={}", i);
                return Result.requestFail("第" + (i +1) + "个菜单主键不能为空");
            }
        }

        roleMenuService.saveRoleMenu(roleId, menuIdList);

        log.info("web => 保存角色关联菜单操作结束");
        return Result.success();
    }

    /**
     * 保存菜单关联角色接口
     * @param roleMenuVo 参数对象
     */
    @Resubmit(spEL = "'addOrModifyRoleMenu-'.concat(#roleMenuVo.menuIdList[0])", message = "保存菜单关联角色")
    @RequestMapping("/addOrModify")
    public Result<Object> addOrModifyRoleMenu(@Validated @RequestBody RoleMenuVo roleMenuVo) {
        log.info("web => 开始保存菜单关联角色操作。{}", roleMenuVo);

        List<Long> roleIdList = roleMenuVo.getRoleIdList();
        Long menuId = roleMenuVo.getMenuIdList().get(0);
        for (int i = 0; i < roleIdList.size(); i++) {
            Long roleId = roleIdList.get(i);
            if (Objects.isNull(roleId)) {
                log.info("角色主键值错误。i={}", i);
                return Result.requestFail("第" + (i +1) + "个角色主键不能为空");
            }
        }
        if (Objects.isNull(menuId)) {
            log.info("菜单主键值错误");
            return Result.requestFail("菜单主键不能为空");
        }

        roleMenuService.addRoleMenu(roleIdList, menuId);

        log.info("web => 保存菜单关联角色操作结束");
        return Result.success();
    }

}
